package com.odt.outdoor;

import android.app.Application;

import com.odt.outdoor.framwork.AppStateTracker;
import com.odt.outdoor.framwork.Trace;
import com.odt.outdoor.locations.LocationService;
import com.odt.outdoor.net.API;
import com.odt.outdoor.utils.GlideImageLoader;
import com.shaomengjie.okhttp.RequestManager;

import java.io.IOException;
import java.util.concurrent.TimeUnit;

import cn.finalteam.galleryfinal.CoreConfig;
import cn.finalteam.galleryfinal.FunctionConfig;
import cn.finalteam.galleryfinal.GalleryFinal;
import cn.finalteam.galleryfinal.ImageLoader;
import cn.finalteam.galleryfinal.ThemeConfig;
import okhttp3.Interceptor;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import okhttp3.logging.HttpLoggingInterceptor;

/**
 * Created by shaomengjie on 2017/3/14.
 */

public class MyApplication extends Application {
    private boolean isDev = true;
    public LocationService locationService;
    private static MyApplication mInstance;
    @Override
    public void onCreate() {
        super.onCreate();
        mInstance = this;
        configApp();
        initImageSelector();
    }

    private void configApp() {
        Trace.model = isDev;
        AppStateTracker.init(this);
        HttpLoggingInterceptor logging = new HttpLoggingInterceptor();
        logging.setLevel(HttpLoggingInterceptor.Level.BODY);
        OkHttpClient client = new OkHttpClient.Builder().addInterceptor(logging)
                .connectTimeout(60, TimeUnit.SECONDS).readTimeout(60, TimeUnit.SECONDS)
                .addInterceptor(new Interceptor() {
                    @Override
                    public Response intercept(Chain chain) throws IOException {
                        Request.Builder newRequest = chain.request().newBuilder();
                        newRequest.addHeader("currentVersion", BuildConfig.VERSION_CODE + ".0");
                        newRequest.addHeader("mobileType", "0");
                        newRequest.addHeader("APIVersion", API.API_VERSION);
                        Response response = chain.proceed(newRequest.build());
                        return response;
                    }
                })
                .build();
        RequestManager.getInstance().init(client);
        locationService = new LocationService(getInstance());
    }

    public static MyApplication getInstance() {
        return mInstance;
    }

    /**
     * 初始化GalleryFinal的配置
     */
    public void initImageSelector(){
        ThemeConfig themeConfig = new ThemeConfig.Builder().setTitleBarBgColor(getResources().getColor(R.color.colorPrimary)).build();
        FunctionConfig functionConfig = new FunctionConfig.Builder().setEnableCamera(true)
                .setEnableCrop(false).setEnableEdit(false).setEnableRotate(false).setEnablePreview(true).build();
        ImageLoader imageloader = new GlideImageLoader();
        CoreConfig coreConfig = new CoreConfig.Builder(mInstance, imageloader, themeConfig)
                .setFunctionConfig(functionConfig).build();
        GalleryFinal.init(coreConfig);
    }

}
